package photorganizer.common.helper;

import static java.util.Collections.EMPTY_LIST;

import java.util.Iterator;

public class CollectionHelper {

	@SuppressWarnings("unchecked")
	public static <T> Iterator<T> iterator(Iterable<T> iterable) {
		return iterable == null ? EMPTY_LIST.iterator() : iterable.iterator();
	}

	public static <T> T getSingle(Iterator<T> iterator) {
		T result = iterator.hasNext() ? iterator.next() : null;
		if (result != null && iterator.hasNext()) {
			throw new IllegalArgumentException("The iteration has more than one element");
		}
		return result;
	}

	public static <T> T getSingle(Iterable<T> iterable) {
		return getSingle(iterable.iterator());
	}

	private CollectionHelper() {
		assert false;
	}

}